﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuantitativeInvestment.Bean;
using QuantitativeInvestment.Tools;

namespace QuantitativeInvestment.Factor
{
    class StochFactor:Factor
    {
        public StochFactor()
        {
            this.name = "随机指标";
            Parameter p1 = new Parameter("慢D周期", 30);
            this.paraList.Add(p1.name,p1);

            Parameter p2 = new Parameter("快K周期", 12);
            this.paraList.Add(p2.name, p2);

            Parameter p3 = new Parameter("慢K周期", 26);
            this.paraList.Add(p3.name, p3);
        }
        public override void addFactorValue(Stock stock)
        {
            int optInFastK_Period = Int32.Parse(this.paraList["快K周期"].value.ToString());
            int optInSlowK_Period = Int32.Parse(this.paraList["慢K周期"].value.ToString());
            int optInSlowD_Period = Int32.Parse(this.paraList["慢D周期"].value.ToString());

            if (!stock.factors.ContainsKey(this.name + this.paraList["慢D周期"].value.ToString()))
            {
                TaLib lib = new TaLib();

                double[,] stochValues = lib.getStoch(stock.factors["最高价"],stock.factors["最底价"], stock.factors["收盘价"],optInFastK_Period,optInSlowK_Period,optInSlowD_Period);
                int length=stock.factors["最高价"].Length;

                double[] result1 = new double[length];
                double[] result2 = new double[length];

                for (int i = 0; i < length; i++)
                {
                    result1[i] = stochValues[0, i];
                    result2[i] = stochValues[1, i];
                }
                stock.factors.Add(this.name + this.paraList["快K周期"].value.ToString() + "快K周期", result1);
                stock.factors.Add(this.name + this.paraList["慢D周期"].value.ToString() + "慢D周期", result2);
                }
        }
    }
}
